package  crl.menu
{
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;
	
	import crl.menu.data.Config;
	import crl.menu.data.Item;
	import crl.menu.events.MenuEvent;
	import crl.menu.utils.DisplayUtils;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	
	public class MainMenu extends EventDispatcher
	{
		private var app:Application;
		private var len:int;
		private var items:Vector.<Item>;
		private var defaultColorTransform:ColorTransform;
		private var overColorTransform:ColorTransform;
		public function MainMenu(app:Application) : void
		{
			this.app=app;
			
			items=Config.instance.mainURLS;
			len=items.length;
			overColorTransform=defaultColorTransform=app.menu0.transform.colorTransform

			overColorTransform.redOffset=50;
			overColorTransform.greenOffset=50;
			overColorTransform.blueOffset=50;

			ColorShortcuts.init();
			
			addEvent();
		}
		
		private function itemOverHandler(event:MouseEvent) : void
		{
			menuOver(MovieClip(event.currentTarget.parent)["idx"]);
		}
		
		private function addEvent() : void
		{
			var i:int=0;
			
			var mc:MovieClip;
			var label:TextField;
			var hit:InteractiveObject;
			
			while (i < len )
			{
				mc=app["menu"+i];
				mc.mouseEnabled=false;
				if(mc.totalFrames>i){
					mc.gotoAndStop(i+1);
				}
				
				label=mc["label"] as TextField;
				if(label){
					var value:String=items[i].label;
					if(value)label.text=value;
				}
				
				
				mc["idx"] = i;
				
				hit=mc["hit"];
				
				DisplayUtils.allMouseEnabled(mc,false,[hit]);
				
				hit.addEventListener(MouseEvent.MOUSE_OVER, itemOverHandler);
				hit.addEventListener(MouseEvent.MOUSE_OUT, itemOutHandler);
				hit.addEventListener(MouseEvent.CLICK, itemClickHandler);
				i++;
			}
		}
		
		
		
		public function menuOver(value:int = -1) : void
		{
			var i:int = 0;
			
			var mc:MovieClip;
			
			while (i <len)
			{
				mc=app["menu"+i];
				
				if(i!=3)Tweener.addTween(mc,{_colorTransform:null, time:0.3});
				
				i++;
			}
			if (value == -1)
			{
				this.dispatchEvent(new MenuEvent(Event.CLOSE,value));
			}
			else
			{
				this.dispatchEvent(new MenuEvent(Event.OPEN,value));
				
				mc=app["menu"+value];
				if(value!=3)Tweener.addTween(mc,{_colorTransform:overColorTransform, time:0.3});
			}
		}
		
		private function itemClickHandler(event:MouseEvent) : void
		{
			Config.instance.toMain(MovieClip(event.currentTarget.parent)["idx"]);
		}
		
		private function itemOutHandler(event:MouseEvent) : void
		{
			menuOver();
		}
		
	}
}
